<script setup>
import { useUserStore } from '@/stores/user'
import { ref } from 'vue'
const userStore = useUserStore()
const userInfo = ref({ ...userStore.userInfo })
const uploadAvatar = (obj) => {
  const render = new FileReader()
  render.readAsDataURL(obj.raw)
  render.onload = () => (userInfo.value.avatar = render.result)
}

const rules = {
  nickname: [{ required: true, message: '昵称不能为空', trigger: 'blur' }],
  email: [
    { required: true, message: '邮箱不能为空', trigger: 'blur' },
    { type: 'email', message: '邮箱格式不正确', trigger: 'blur' }
  ],
  phone: [
    { required: true, message: '手机号不能为空', trigger: 'blur' },
    { type: 'phone', message: '手机号格式不正确', trigger: 'blur' }
  ],
  avatar: [{ required: true, message: '请上传头像', trigger: 'change' }]
}
const form = ref()

const reset = () => {
  userInfo.value = { ...userStore.userInfo }
}
const submit = async () => {
  await form.value.validate()
  userStore.update(userInfo.value)
}
</script>

<template>
  <div class="container">
    <el-form
      :label-position="'top'"
      ref="form"
      label-width="auto"
      :model="userInfo"
      :rules="rules"
    >
      <el-form-item label="头像:" prop="avatar">
        <el-upload
          class="avatar-uploader"
          action="#"
          :show-file-list="false"
          :auto-upload="false"
          :on-change="uploadAvatar"
        >
          <img
            v-if="userInfo.avatar"
            width="100"
            height="100"
            :src="userInfo.avatar"
          />
          <el-icon v-else size="100" color="gray">
            <Plus />
          </el-icon>
        </el-upload>
      </el-form-item>
      <el-form-item label="账户:">
        <el-input
          :modelValue="userInfo.username"
          style="width: 240px"
          type="text"
          disabled
        />
      </el-form-item>
      <el-form-item label="昵称:" prop="nickname">
        <el-input
          v-model="userInfo.nickname"
          style="width: 240px"
          type="text"
        />
      </el-form-item>
      <el-form-item label="邮箱:" prop="email">
        <el-input v-model="userInfo.email" style="width: 240px" type="text" />
      </el-form-item>
      <el-form-item label="手机号:" prop="phone">
        <el-input v-model="userInfo.phone" style="width: 240px" type="text" />
      </el-form-item>
      <el-form-item>
        <el-button @click="reset">重置</el-button>
        <el-button type="primary" @click="submit">更新</el-button>
      </el-form-item>
    </el-form>
    <div class="logo">
      <img src="@/assets/img/logo.png" width="200" alt="" />
      <h1>更加快捷，更加方便，一键管理</h1>
      <img
        src="@/assets/img/pic.png"
        style="mix-blend-mode: difference"
        width="100%"
        alt=""
      />
    </div>
  </div>
</template>

<style scoped>
h1 {
  color: white;
  text-align: center;
  font-size: 20px;
  letter-spacing: 20px;
}

.logo {
  background-color: #220d4e;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

.logo img {
  display: block;
  margin: 0 auto;
}

.container {
  height: 100%;
  width: 100%;
  display: grid;
  grid-template-columns: 1fr 1fr;
  overflow: hidden;
}

form {
  background-color: #f5f6f8;
  display: flex;
  width: 100%;
  justify-content: center;
  flex-direction: column;
  align-items: center;
}
</style>
